<?php

	$value_sensor1 = exec('cat /sys/bus/w1/devices/28-000004509084/w1_slave');
	$value_sensor2 = exec('cat /sys/bus/w1/devices/28-00000450a152/w1_slave');
	$value_sensor3 = exec('cat /sys/bus/w1/devices/28-00000450adf4/w1_slave');
	$value_sensor4 = exec('cat /sys/bus/w1/devices/28-00000450b2a6/w1_slave');
	$value_sensor5 = exec('cat /sys/bus/w1/devices/28-00000450f733/w1_slave');
	$value_sensor6 = exec('tail -n 1 /var/www/scripts/terra_control/log/Terra1');
	$value_sensor7 = exec('cat /sys/class/hwmon/hwmon1/device/humidity1_input');

	//Sensor location,name (e.g. aussen)
	$location= $_POST["pot0"];
	//Zweiter Sensor
	$location2= $_POST["pot1"];
	
	//Zeitraum 
	$duration= $_POST["pot2"];
	
	
	$both=$location."_".$duration;
	$both2=$location2."_".$duration;
	
	$plot_file="scripts/plotter/plotfiles/$both.png";
	
	$source_file="scripts/terra_control/log/$location";
	$source_file2="scripts/terra_control/log/$location2";

	$box_width=680;
	$box_height=420;

	

	// TIME CALCULATION
	// Calculate the variables for the gnuplot xrange!
	// YYYY-mm-dd-HH-MM
	$dates = date('Y-m-d-H-i');
	echo "Datum: $dates<br>";
	$plot_time_end = "$dates";
	

	//plot time last hour
	$dates = date('Y-m-d-H-i', strtotime('-1 hours'));

	//date_arr
	//##################	
	//# [0] : Year 	   #
	//# [1] : month    # 
	//# [2] : dayofmon #
	//# [3] : hour     #
	//# [4] : min      #
    //##################
	$date_arr = explode("-",$dates);
	$plot_timehour_begin = "{$date_arr[0]}-{$date_arr[1]}-{$date_arr[2]}-{$date_arr[3]}-00" ;
	//echo "plot_time_begin=$plot_timehour_begin<br>";


	

	// plot time last day
	$dates = date('Y-m-d-H-i', strtotime('-1 day'));

	
	$date_arr = explode("-",$dates);
	$plot_timeday_begin = "{$date_arr[0]}-{$date_arr[1]}-{$date_arr[2]}-00-00" ;
	//echo "plot_time_begin=$plot_timeday_begin<br>";
	


	// plot time last week
	$dates = date('Y-m-d-H-i', strtotime('-1 week'));
	$date_arr = explode("-",$dates);
	$plot_timeweek_begin = "{$date_arr[0]}-{$date_arr[1]}-{$date_arr[2]}-00-00" ;
	//echo "plot_time_begin=$plot_timeweek_begin<br>";
?>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title> Terrarien Steuerung </title>
  <meta name="description" content="">
  <meta name="keywords" content="">
  <link rel="stylesheet" href="style.css" type="text/css">
</head>

<body>

<h3>Sensorwerte Terrarium 1:</h3>

<table border=3>
   <tr>
		<th> Au&szlig;en: </th> 
		<td> <?php echo "$value_sensor1[0]$value_sensor1[1].$value_sensor1[2] &deg;C" ?></td>
		
   </tr>
		
   <tr>
		<th> Kaltstelle: </th> 
		<td> <?php echo "$value_sensor2[0]$value_sensor2[1].$value_sensor2[2] &deg;C" ?></td>
   </tr>

	<tr>
		<th> Oben: </th> 
		<td> <?php echo "$value_sensor3[0]$value_sensor3[1].$value_sensor3[2] &deg;C" ?></td>
   </tr>

	<tr>
		<th> Unten: </th> 
		<td> <?php echo "$value_sensor4[0]$value_sensor4[1].$value_sensor4[2] &deg;C" ?></td>
   </tr>

	<tr>
		<th> Sonne: </th> 
		<td> <?php echo "$value_sensor5[0]$value_sensor5[1].$value_sensor5[2] &deg;C" ?></td>
   </tr>
	<tr>
		<th> Durchschnitt: </th> 
		<td> <?php echo "$value_sensor6[0]$value_sensor6[1].$value_sensor6[2] &deg;C" ?></td>
   </tr>
	<tr>
		<th> Feuchtigkeit: </th> 
		<td> <?php echo "$value_sensor7[0]$value_sensor7[1].$value_sensor7[2] %" ?></td>
   </tr>
</table>


	


<h3>Visualisierung des Temperaturverlaufs:</h3>
<!--<h3> <?php echo $plot_file ; ?> </h3>!-->
<!--<h3> <?php echo $source_file ; ?> </h3>!-->
<!--<h3> <?php echo $source_file2 ; ?> </h3>!-->
<!--<li>scripts/plotter/plotfiles/aussen_lasth.png</li>!-->


<form action="datalognew.php" method="post">
  <p>
	<table border=3>
  <tr>
  	<td>
    <select name="pot0" size="6" multiple>
      <option value="Terra1">Durchschnitt</option>
      <option value="aussen">Au&szlig;en</option>
      <option value="oben">Oben</option>
      <option value="unten">Unten</option>
      <option value="sonne">Sonne</option>
	  <option value="kalt">Kaltstelle</option>
	  <option value="feucht">Feuchtigkeit</option>
    </select>
  	</td>
	<td>
    <select name="pot1" size="6" multiple>
      <option value="Terra1">Durchschnitt</option>
      <option value="aussen">Au&szlig;en</option>
      <option value="oben">Oben</option>
      <option value="unten">Unten</option>
      <option value="sonne">Sonne</option>
	  <option value="kalt">Kaltstelle</option>
	  <option value="feucht">Feuchtigkeit</option>
    </select>
  	</td>
	<td>
      <input type="radio" name="pot2" value="lasth">Stunde
    </td>
	<td>
      <input type="radio" name="pot2" value="lastd">Tag
    </td>
	<td>
      <input type="radio" name="pot2" value="lastw">Woche
    </td>

  </tr>
  </table>
	<input type="submit" name="case" value="Auswahl anzeigen">
 </p>
</form>

<?php

 if ( $location != "" )
 {
	
	if ($duration == "lasth") {	

		$plot_begin = $plot_timehour_begin;
		$set_format_x = "%M";

    }elseif ($duration == "lastd") {
		$plot_begin = $plot_timeday_begin;
	 	$set_format_x = "%d/%H:%M";	
	}elseif ($duration == "lastw") {
		$plot_begin = $plot_timeweek_begin;
		$set_format_x = "%d";
	}	


	$datei = fopen("scripts/plotter/.tmpplot","w");
	
		$write_string="set terminal png\n";
		fwrite($datei, $write_string);

		$write_string="set output \"$plot_file\"\n";
		fwrite($datei, $write_string);
		
		$write_string="set xdata time\n";
		fwrite($datei, $write_string);
		
		$write_string="set timefmt \"%Y-%m-%d-%H-%M\"\n";
		fwrite($datei, $write_string);

		//Set time x-axis format
		$write_string="set format x \"$set_format_x\"\n";
		fwrite($datei, $write_string);

		$write_string="set yrange [5:50]\n";
		fwrite($datei, $write_string);
		
		$write_string="set ytics 5\n";
		fwrite($datei, $write_string);
		
		
		$write_string="set xrange [\"$plot_begin\" : \"$plot_time_end\" ]\n";
		fwrite($datei, $write_string);

		$write_string="set xtics rotate\n";
		fwrite($datei, $write_string);

		$write_string="set xlabel \"Zeit\"\n";
		fwrite($datei, $write_string);

		$write_string="set ylabel \"Temperatur [Celsius]\"\n";
		fwrite($datei, $write_string);
	
		$write_string="set grid\n";
		fwrite($datei, $write_string);	
		
		
		$write_string="plot \"$source_file\" using 1:2 with lines title \"$location\"";
		fwrite($datei, $write_string);	

    //Was a second location selected?!
	if ( $location2 != "" )
	{
		$write_string=", \"$source_file2\" using 1:2 with lines title \"$location2\"\n";
		fwrite($datei, $write_string);
	}	
	fclose(datei);
		
	// Priint errors in logfile
	system('gnuplot /var/www/scripts/plotter/.tmpplot 2> scripts/plotter/plotter.log',$result);



	// Fehlerausgabe !!

	if ($result == "1")
	{
		echo "<h3> Fehler beim erstellen der Visualisierung </h3>";
		echo "Kommando Datei f&#252;r gnuplot<br>";
		
		$plottmpfile = system("cat scripts/plotter/.tmpplot | tr \"\n\" '?' | sed -e 's/?/<br>/g'");
		echo $plottmpfile;
		$plotlogfile = system("cat scripts/plotter/plotter.log | tr \"\n\" '?' | sed -e 's/?/<br>/g'");
		echo $plotlogfile;
	} 
	echo "$result";	
 	echo "<img id=\"plot\" src=\"$plot_file\" width=\"$box_width\" height=\"$box_height\">\n";
	
	/**********************************/
 }

?>	







</body>
</html>
